﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Atc600Control
{
    public class Atc600
    {
        private TcpClient Client;
        public void Connect()
        {
            try
            {
                Client = new TcpClient("192.168.0.123", 502);
                SendStream = Client.GetStream();

                byte[] unit = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x00, 0x0F, 0x00, 0x02 };
                SendStream.Write(unit, 0, unit.Length);
                Thread.Sleep(10);

                byte[] decimalpoint = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x00, 0x09, 0x00, 0x01 };
                SendStream.Write(decimalpoint, 0, decimalpoint.Length);
                Thread.Sleep(10);

                byte[] on = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x00, 0x17, 0x00, 0x00 };
                SendStream.Write(on, 0, on.Length);
                Thread.Sleep(10);

                SendStream.Flush();
            }
            catch (Exception ex)
            {
            }
        }
        public void Closed()
        {
            try
            {
                SendStream = Client.GetStream();
                byte[] off = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x00, 0x17, 0x00, 0x01 };
                SendStream.Write(off, 0, off.Length);
                SendStream.Flush();
                SendStream.Close();
                ReadStream.Close();
                if (Client.Connected)
                    Client.Close();
            }
            catch (Exception)
            {
            }
        }

        private NetworkStream SendStream;
        public void SetTension(int hand_or_outo, int tension)
        {

            if (hand_or_outo == 0)//手动
            {
                tension = tension * 2;//N转换为电平

                SendStream = Client.GetStream();

                byte[] handtension = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x00, 0x19, 0x00, (byte)tension };
                SendStream.Write(handtension, 0, handtension.Length);
                Thread.Sleep(10);
                byte[] handmode = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x00, 0x13, 0x00, 0x00 };
                SendStream.Write(handmode, 0, handmode.Length);
                SendStream.Flush();
            }
            else if (hand_or_outo == 1)//PID
            {
                tension = tension * 10;//N转换为N
                SendStream = Client.GetStream();

                byte[] outotension = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x00, 0x12, 0x00, (byte)tension };
                SendStream.Write(outotension, 0, outotension.Length);
                Thread.Sleep(10);
                byte[] outomode = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x00, 0x13, 0x00, 0x01 };
                SendStream.Write(outomode, 0, outomode.Length);
                SendStream.Flush();
            }
        }

        private NetworkStream ReadStream;
        public double read_tension()
        {
            double tension = 0;
            ReadStream = Client.GetStream();
            //byte[] readPID = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x02, 0x00, 0x01 };
            byte[] readsenser = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x01, 0x00, 0x01 };
            SendStream.Write(readsenser, 0, readsenser.Length);
            byte[] bytes = new byte[1024];
            int bytesread = ReadStream.Read(bytes, 0, bytes.Length);
            if (bytes[0] == 0x00 && bytes[1] == 0x01 && bytes[2] == 0x00 && bytes[3] == 0x00 && bytes[4] == 0x00 &&
                bytes[5] == 0x05 && bytes[6] == 0x01 && bytes[7] == 0x03 && bytes[8] == 0x02)
            {
                tension = bytes[9] * 0x100 + bytes[10];
                tension = tension / 10;//张力传感器实时值 单位N
            }
            SendStream.Flush();
            return tension;
        }
    }
}
